package com.huluobo.high3videoproject.adapter;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.media.Image;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.huluobo.high3videoproject.R;
import com.huluobo.high3videoproject.bean.VideoBeanResult;
import com.huluobo.high3videoproject.utils.GlideUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.List;

public class VideoFullscreenAdapter extends BaseQuickAdapter<VideoBeanResult.VideoBean, BaseViewHolder> {

    public VideoFullscreenAdapter(@Nullable List<VideoBeanResult.VideoBean> data) {
        super(R.layout.item_video_fullscreen, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, VideoBeanResult.VideoBean videoBean) {
        //设置视频路径
        StandardGSYVideoPlayer standardGSYVideoPlayer = baseViewHolder.getView(R.id.item_gsy_video_player);
        standardGSYVideoPlayer.setUp(videoBean.getVideopath(), true, "");
        standardGSYVideoPlayer.setThumbImageView(getThumView(videoBean.getVideomainimg()));//设置封面图
        standardGSYVideoPlayer.getBackButton().setVisibility(View.GONE);//设置播放器的返回按钮消失
        if (getItemPosition(videoBean) == 0) {
            standardGSYVideoPlayer.startPlayLogic();
        }

        //是否关注
        //获取bean中的关注值,等于1代表已经关注,让add按钮消失,等于0代表没有关注,让add按钮展示
        if (videoBean.getGuanzhu() == 1) {
            baseViewHolder.getView(R.id.item_video_iv_add).setVisibility(View.GONE);
        } else {
            baseViewHolder.getView(R.id.item_video_iv_add).setVisibility(View.VISIBLE);
        }

        //是否点赞
        if (videoBean.getDianzan() == 1) {
            baseViewHolder.setImageResource(R.id.item_video_iv_like_btn, R.drawable.ic_xihuanred);
        } else {
            baseViewHolder.setImageResource(R.id.item_video_iv_like_btn, R.drawable.ic_xihuan);
        }

        //设置头像
        ImageView headView = baseViewHolder.getView(R.id.item_video_iv_head);
        GlideUtils.load(getContext(), headView, videoBean.getHeadpath(), 0, 0, 100);

        //设置点赞数
        int likeCount = videoBean.getLike_count();
        if (likeCount > 10000) {
            baseViewHolder.setText(R.id.item_video_tv_like_count, likeCount / 10000 + "w");
        } else {
            baseViewHolder.setText(R.id.item_video_tv_like_count, likeCount + "");
        }

        //设置收藏
        if (videoBean.getDianzan() == 1) {
            baseViewHolder.setImageResource(R.id.item_video_iv_save_btn, R.drawable.ic_shoucangyellow);
        } else {
            baseViewHolder.setImageResource(R.id.item_video_iv_save_btn, R.drawable.ic_shoucang);
        }

        //设置头像旋转
        ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(headView, "Rotation", 0, 360);
        rotateAnim.setDuration(5000);
        rotateAnim.setRepeatCount(ValueAnimator.INFINITE);//设置无限循环
        rotateAnim.setInterpolator(new LinearInterpolator());//匀速效果
        rotateAnim.start();

        //设置左下角文本
        baseViewHolder.setText(R.id.item_video_desc, videoBean.getCaption() + "");
        baseViewHolder.setText(R.id.item_video_username, videoBean.getAuthname() + "");
        if (videoBean.getAddress() == null) {
            baseViewHolder.getView(R.id.item_video_address).setVisibility(View.GONE);
            baseViewHolder.getView(R.id.item_video_local).setVisibility(View.GONE);
        } else {
            baseViewHolder.setText(R.id.item_video_address, videoBean.getAddress() + "");
            baseViewHolder.getView(R.id.item_video_local).setVisibility(View.VISIBLE);
        }
        baseViewHolder.setText(R.id.item_video_caption, videoBean.getCaption() + "");
        //跑马灯效果
        baseViewHolder.getView(R.id.item_video_desc).requestFocus();//请求获取焦点
    }

    //展示缩略图,因为GsyVideoPlayer只接受一个imageview作为缩略图
    private View getThumView(String videomainimg) {
        ImageView imageView = new ImageView(getContext());//使用代码生一个ImageView
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);//缩放模式,目前为中心缩放
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);//设置宽高必须要使用LayoutParams来设置宽高,不能直接设置
        imageView.setLayoutParams(params);
        GlideUtils.loadNormal(getContext(), imageView, videomainimg);
        return imageView;
    }


}
